Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = /* */function () {
  function _class21() {
    babel_classCallCheck(this, _class21);
  }
  return babel_createClass(_class21, null, [{
    key: "splitPolygonByLine",
    value: function splitPolygonByLine(e, t, n) {
      var o = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
      var i = [];
      for (var s = n.length - 1; s >= 0; s--) {
        var _o74 = n[s];
        var a = 0 == s ? n[n.length - 1] : n[s - 1];
        var _this$lineCrossPoint = this.lineCrossPoint(_o74, a, e, t);
        var _this$lineCrossPoint2 = babel_slicedToArray(_this$lineCrossPoint, 2);
        var r = _this$lineCrossPoint2[0];
        var l = _this$lineCrossPoint2[1];
        if (-1 != r) {
          n.splice(s, -1, l);
          i.push(s + 1);
        }
      }
      return i;
    }
  }, {
    key: "lineCrossPoint",
    value: function lineCrossPoint(e, t, n, o) {
      var i;
      var s;
      var a;
      var r;
      var l;
      var c;
      var d;
      var h;
      var u = e;
      var g = t;
      var f = n;
      var p = o;
      var m = new cc.Vec2(0, 0);
      l = this.dblcmp(i = this.ab_cross_ac(u, g, f), 0);
      c = this.dblcmp(s = this.ab_cross_ac(u, g, p), 0);
      d = this.dblcmp(a = this.ab_cross_ac(f, p, u), 0);
      h = this.dblcmp(r = this.ab_cross_ac(f, p, g), 0);
      if (-2 == (l ^ c) && -2 == (d ^ h)) {
        return m.x = (f.x * s - p.x * i) / (s - i), m.y = (f.y * s - p.y * i) / (s - i), [0, m];
      } else {
        if (0 == l && this.point_on_line(f, u, g) <= 0) {
          return [1, m = f];
        } else {
          if (0 == c && this.point_on_line(p, u, g) <= 0) {
            return [1, m = p];
          } else {
            if (0 == d && this.point_on_line(u, f, p) <= 0) {
              return [1, m = u];
            } else {
              if (0 == h && this.point_on_line(g, f, p) <= 0) {
                return [1, m = g];
              } else {
                return [-1, null];
              }
            }
          }
        }
      }
    }
  }, {
    key: "isLineSegmentCross",
    value: function isLineSegmentCross(e, t, n, o) {
      return ((n.x - e.x) * (n.y - o.y) - (n.y - e.y) * (n.x - o.x)) * ((n.x - t.x) * (n.y - o.y) - (n.y - t.y) * (n.x - o.x)) < 0 || ((e.x - n.x) * (e.y - t.y) - (e.y - n.y) * (e.x - t.x)) * ((e.x - o.x) * (e.y - t.y) - (e.y - o.y) * (e.x - t.x)) < 0;
    }
  }, {
    key: "point_on_line",
    value: function point_on_line(e, t, n) {
      return this.dblcmp(this.dot(t.x - e.x, t.y - e.y, n.x - e.x, n.y - e.y), 0);
    }
  }, {
    key: "rayPointToLine",
    value: function rayPointToLine(e, t, n) {
      Math.min(t.x, n.x);
      var o = Math.max(t.x, n.x);
      var i = Math.min(t.y, n.y);
      var s = Math.max(t.y, n.y);
      if (e.y < i || e.y > s || e.x > o) {
        return -1;
      }
      var a = t.x + (n.x - t.x) / (n.y - t.y) * (e.y - t.y);
      if (a > e.x) {
        return 0;
      } else {
        if (a == e.x) {
          return 1;
        } else {
          return -1;
        }
      }
    }
  }, {
    key: "relationPointToPolygon",
    value: function relationPointToPolygon(e, t) {
      var n = 0;
      for (var o = 0; o < t.length; ++o) {
        if (t[o].equals(e)) {
          return 2;
        }
        var i = t[o];
        var s = t[0];
        o < t.length - 1 && (s = t[o + 1]);
        var a = this.rayPointToLine(e, i, s);
        if (1 == a) {
          return 1;
        }
        0 == a && n++;
      }
      if (n % 2 == 0) {
        return -1;
      } else {
        return 0;
      }
    }
  }, {
    key: "Polygonintersect",
    value: function Polygonintersect(e, t) {
      for (var _n34 = 0; _n34 < e.length; _n34++) {
        if (0 == this.relationPointToPolygon(e[_n34], t)) {
          return true;
        }
      }
      return false;
    }
  }, {
    key: "PolygoninP",
    value: function PolygoninP(e, t) {
      for (var _n35 = 0; _n35 < e.length; _n35++) {
        if (-1 == this.relationPointToPolygon(e[_n35], t)) {
          return false;
        }
      }
      return true;
    }
  }, {
    key: "getCenterOfGravity",
    value: function getCenterOfGravity(e) {
      var t = 0;
      var n = 0;
      var o = 0;
      var i = e[1];
      for (var r = 2; r < e.length; r++) {
        var _s8 = e[r];
        var _a19 = this.ComputePolygonArea([e[0], i, _s8]);
        o += _a19;
        t += (e[0][0] + i[0] + _s8[0]) * _a19;
        n += (e[0][1] + i[1] + _s8[1]) * _a19;
        i = _s8;
      }
      var s = t / o / 3;
      var a = n / o / 3;
      return cc.v2(s, a);
    }
  }, {
    key: "lineCutPolygon",
    value: function lineCutPolygon(e, t, n) {
      var o = [];
      var i = [];
      var s = [];
      for (var a = 0; a < n.length; ++a) {
        i.push(n[a]);
        var _o75 = n[a];
        var r = n[0];
        a < n.length - 1 && (r = n[a + 1]);
        var l = this.lineCrossPoint(e, t, _o75, r);
        if (0 == l[0]) {
          s.push(i.length);
          i.push(l[1]);
        } else if (l[0] > 0) {
          if (l[1].equals(_o75)) {
            s.push(i.length - 1);
          } else {
            s.push(i.length);
          }
        }
      }
      if (s.length > 1) {
        var _e34 = i[s[0]];
        var _t35 = i[s[1]];
        if (!_e34 || !i[s[s.length - 1]]) {
          return [];
        }
        var _a20 = this.relationPointToPolygon(new cc.Vec2((_e34.x + _t35.x) / 2, (_e34.y + _t35.y) / 2), n);
        var _r3 = _a20 >= 0;
        if (s.length > 2 && _e34.sub(_t35).mag() > _e34.sub(i[s[s.length - 1]]).mag()) {
          _t35 = i[s[s.length - 1]];
          _r3 = (_a20 = this.relationPointToPolygon(new cc.Vec2((_e34.x + _t35.x) / 2, (_e34.y + _t35.y) / 2), n)) < 0;
        }
        var _l2 = _r3;
        var c = 0;
        var d = s[c];
        var h = [];
        var u = [];
        var g = 0;
        h.push(i[d]);
        _r3 && u.push(i[d]);
        c++;
        g++;
        for (d++; g < i.length;) {
          d == i.length && (d = 0);
          var _e35 = i[d];
          if (c >= 0 && d == s[c]) {
            ++c >= s.length && (c = 0);
            if (_r3) {
              u.push(_e35), o.push(u), u = [];
            } else {
              (u = []).push(_e35);
            }
            h.push(_e35);
            _r3 = !_r3;
          } else if (_r3) {
            u.push(_e35);
          } else {
            h.push(_e35);
          }
          d++;
          g++;
        }
        if (_r3) {
          if (!_l2 && u.length > 1) {
            u.push(i[s[0]]);
            o.push(u);
          } else {
            for (var _n36 = 0; _n36 < u.length; ++_n36) {
              h.push(u[_n36]);
            }
          }
        }
        o.push(h);
      }
      return o;
    }
  }, {
    key: "LinearIntegration",
    value: function LinearIntegration(e, t) {
      return .5 * (t.x - e.x) * (t.y + e.y);
    }
  }, {
    key: "ComputePolygonArea",
    value: function ComputePolygonArea(e) {
      var t = e.length;
      if (t < 3) {
        return 0;
      }
      var n = e[0].y * (e[t - 1].x - e[1].x);
      for (var o = 1; o < t - 1; ++o) {
        n += e[o].y * (e[o - 1].x - e[o + 1].x);
      }
      n += e[t - 1].y * (e[t - 2].x - e[0].x);
      return Math.abs(.5 * n);
    }
  }, {
    key: "dcmp",
    value: function dcmp(e) {
      if (e > 1e-6) {
        return 1;
      } else {
        if (e < -1e-6) {
          return -1;
        } else {
          return 0;
        }
      }
    }
  }, {
    key: "cross3",
    value: function cross3(e, t, n) {
      return (e.x - n.x) * (t.y - n.y) - (t.x - n.x) * (e.y - n.y);
    }
  }, {
    key: "intersection",
    value: function intersection(e, t, n, o) {
      var i = e;
      var s = ((e.x - n.x) * (n.y - o.y) - (e.y - n.y) * (n.x - o.x)) / ((e.x - t.x) * (n.y - o.y) - (e.y - t.y) * (n.x - o.x));
      i.x += (t.x - e.x) * s;
      i.y += (t.y - e.y) * s;
      console.log("交点：", i);
      return i;
    }
  }, {
    key: "convertPos2Vec2",
    value: function convertPos2Vec2(e, t) {
      var n = [];
      for (var o = 0; o < e.length && o < t; o++) {
        n.push(cc.v2(e[o].x, e[o].y));
      }
      return n;
    }
  }, {
    key: "CPIA",
    value: function CPIA(e, t) {
      var n;
      var o;
      var i;
      var s = [];
      var a = [];
      var r = e.length;
      var l = t.length;
      s = this.convertPos2Vec2(t, l);
      for (var c = 0; c < r && l > 2; c++) {
        o = c == r - 1 ? this.dcmp(this.cross3(e[0], s[0], e[c])) : this.dcmp(this.cross3(e[c + 1], s[0], e[c]));
        for (var _t36 = n = 0; _t36 < l; _t36++, o = i) {
          o >= 0 && (a[n++] = s[_t36]);
          -2 == (o ^ (i = c == r - 1 ? _t36 == l - 1 ? this.dcmp(this.cross3(e[0], s[0], e[c])) : this.dcmp(this.cross3(e[0], s[_t36 + 1], e[c])) : _t36 == l - 1 ? this.dcmp(this.cross3(e[c + 1], s[0], e[c])) : this.dcmp(this.cross3(e[c + 1], s[_t36 + 1], e[c])))) && (a[n++] = c == r - 1 ? _t36 == l - 1 ? this.intersection(e[c], e[0], s[_t36], s[0]) : this.intersection(e[c], e[0], s[_t36], s[_t36 + 1]) : _t36 == l - 1 ? this.intersection(e[c], e[c + 1], s[_t36], s[0]) : this.intersection(e[c], e[c + 1], s[_t36], s[_t36 + 1]));
        }
        (s = this.convertPos2Vec2(a, n))[l = n] = s[0];
      }
      if (l < 3) {
        return 0;
      } else {
        return this.ComputePolygonArea(s);
      }
    }
  }, {
    key: "ab_cross_ac",
    value: function ab_cross_ac(e, t, n) {
      return this.cross(t.x - e.x, t.y - e.y, n.x - e.x, n.y - e.y);
    }
  }, {
    key: "dot",
    value: function dot(e, t, n, o) {
      return e * n + t * o;
    }
  }, {
    key: "cross",
    value: function cross(e, t, n, o) {
      return e * o - n * t;
    }
  }, {
    key: "dblcmp",
    value: function dblcmp(e, t) {
      if (Math.abs(e - t) <= 1e-6) {
        return 0;
      } else {
        if (e > t) {
          return 1;
        } else {
          return -1;
        }
      }
    }
  }]);
}();